In this video we will learn some more application soft rolling statistics and finance in the last video
we have learned how to calculate simple moving averages for stock prices and in this video we will have
a look at the rolling performance of stocks and financial securities and we've already learned that
the risk and return are the most important performance metrics for financial securities like stocks
and in the finance and investment funds industry.
It's quite standard to report risk and return of financial securities for the last three years based
on monthly data.
So that means that the return is reported as the annualized average monthly return over the last the
thirty six month period and the risk is typically reported as the annualized standard deviation of monthly
returns over that period.
So in the next minutes he will calculate rolling risk and return for the S&P 500 and we will find out
if and to what extent risk and to try and change over time.
All right.
So let's start.
And first of all we import pandas an umpire and also met plot lip as always.
Then we are working with the S&P 500 see a sweet fire and of course we want to pass um the date column
to a date time index and we only want to use them.
The column stayed and the closing prices and uh actually we save for the data frame and the variable
S&P 500.
So let's have a look here.
So these are the first five rows starting at the very last day of the year nineteen hundred and seventy.
And as I said before it's quite standard in uh the finance and investment industry to report monthly
returns.
So as we have here daily data or daily timestamps with daily price information first of all we need
to read sample our data frame to monthly and we can do the same with other release sample method and
we too see a monthly and actually you want to have the very last trading price of each month.
And finally we also past the period to the kind parameter.
So let's run through the sell and let's have a look.
So here we have monthly price data.
And on the left hand side we have now a period index as we pass superior to the kind parameter.
So we have a December nineteen hundred seventy.
January nineteen hundred seventy one and two we have our column close.
And here we have for the very last trading price of each month.
So now we have monthly data and we want to calculate monthly returns.
And we can do this here with the percentage change method.
So we change here the read sample method.
The last method and the percentage change method.
And finally we want to drop them all rows with an A values.
So if you calculate the percentage change then in the very first row we will have a uh and a value.
So therefore we you see also the drop in a method.
So we are calculating your monthly returns and we save.
The resulting data frame and the variable monthly returns.
So let's do this here and let's have a look
so here we have the monthly returns.
So for example the return in January in nineteen hundred seventy one was 4 percent.
So this is exactly the change from ninety two point one five to ninety five point eighty seven.
So this is a monthly return of 4 percent.
And actually here now a new data for a monthly return we have actually the monthly returns starting
from January nineteen hundred seventy one and to December 2000 and 18.
Now we want to calculate rolling statistics.
So we want to calculate the mean are the average return of the immediately preceding 36 months.
And we want to do this for timestamps here and therefore we use here the rolling method and the window
is a thirty six.
So on each timestamp we actually group uh the last the 36 month and then we change the mean method to
calculate the mean return.
The mean rolling return.
And by doing so we would end up with average monthly returns.
And it's also quite standard in the finance industry to annualized returns.
So to have kind of a feeling how the return would look like over a complete year and therefore we multiply
the monthly rolling returns by twelve.
So let's do this here.
Consequently we get here now the annualized average monthly returns over the last uh thirty six periods
and in kind of a rolling statistics manner.
So we have for each timestamp actually the annualized mean return for the last three years.
And finally we create in the new column return so let's do this here.
So now we have added the rolling return of the S&P 500 index and now we want to add also the year risk
and um the risk is typically calculated as a standard deviation of monthly returns and we can actually
simply calculate the rolling standard deviation of monthly returns by a take here.
The monthly returns based on the close price then we change the rolling method and of course also here
we pass um thirty six to the window parameter and then we calculate a standard deviation.
So this would uh then reside in the standard deviation of monthly returns.
So to say the monthly standard deviation.
And finally we can also annualize a monthly standard to creation by multiplying with the square root
of twelve.
So let's do this here.
So here we have the rolling standard deviation and finally we can also create a new column risk.
So now we have two new columns the return and risk for our monthly return data frame and then we could
also drop and a rose.
So the first the thirty five rows should contain and a values in both new columns.
So let's do this here and now let's finally inspect our monthly return data frame.
So here we have the monthly returns of the closing prices.
Then we have uh the rolling return.
So this is uh the return of the last uh 36 months.
And we have here the rolling risk.
And apparently he had the first rolling such as Six we have on December nineteen hundred seventy three.
And we could say that um the annualized average monthly return for the preceding three years is actually
two point six percent.
And the risk is twelve percent.
So let's also have a look at the last five rows here and we could say that the annualized average monthly
return from January to 16 to December to eighteen is seven point one percent and the annualized standard
deviation of monthly returns in this period is 11 percent.
Now we can also plot the rolling statistics so the column return and risk and we can do this year of
course with the plot method and we take the last two columns of our data for a monthly returns.
So let's have a look
and here you can actually see that change of Risk and Return of the S&P 500 index over time.
So we have uh the blue line or the blue curve which is the rolling return for the last three years.
And the green line is actually the risk or the standard deviation of monthly returns for the preceding
uh three years period.
And we can also see here that depending on the market environment and depending on the time.
So risk and the return are quite volatile here.
So for example here in 2009 2010 we can see here a quite high risk and a very low return.
So minus 10 percent annualized and the opposite.
We can see for example here in the middle or the end of the 90s.
So we have quite high returns.
So what 20 percent.
And we have quite a low risk here.
So it seems like investing in the S&P 500 over a period of three years is quite volatile and uh really
depends on the market environment and the time you are investing so it can be a good investment.
But it can also be a very poor investment.
And from the two lines see we can also see that the risk and return could be negatively correlated.
So whenever the risk is quite high.
So for example here or here then the returns are pretty low and vice versa actually.
So here we have a higher return and a fairly low risk.
And also here we have a high return and the risk is fairly low.
So it might be worth it to also calculate the correlation between the rolling return and the rolling
risk.
And we can do this with the car method.
So we take the two columns here of monthly returns.
So the last two columns and the change the car method.
So let's do this and we can see here that there is a significant negative correlation between risk and
return.
So minus 0 point.
3 2.
And we can also create a scatter plot with all parcels of risk and return for the last 40 years.
So we plotted the columns the risk and return.
And we use here a scatter plot and on the x axis we want to have the risk a standard deviation and on
the y axis the return.
So let's do this here
so here we have the risk on the x axis and the return on the y axis.
And actually each point is a combination of rolling risk and rolling return for a particular month from
nineteen hundred seventy three until 2018.
So for example we have quite good three year periods with a quiet low risk.
So twelve percent and a high return over 20 percent.
And we can also see periods with a quite poor performance of the S&P 500.
So for example a fairly high standard deviation or risk.
So what 20 percent and a mean return of less than minus 10 percent.
So again we found out that investing in the S&P 500 over a period of three years is a quite risky and
volatile investment.
And in the next two videos we want to investigate how this picture changes if we increase our investment
period also decrease our investment period.
So I hope to see you also in the next videos by.